package com.cloud.pole.rpc;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.pole.api.shop.ShopShopsViewApi;
import com.cloud.pole.api.weixin.WxUserApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.tool.service.FileUploadService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/file")
public class FileUploadRest {

    @Autowired
    private FileUploadService fileUploadService;
    
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    public ApiRestResponse<String> uploadImage(
  		  @RequestParam(name="btImage", defaultValue = "") byte[] btImage,
  		  @RequestParam(name="useType", defaultValue = "") String useType,
  		  @RequestParam(name="fileType", defaultValue = "") String fileType){
		try {
			ParamException.err(StringUtils.isBlank(useType), "useType不能为空");
			ParamException.err(StringUtils.isBlank(fileType), "fileType不能为空");
			ParamException.err(btImage == null, "btImage不能为空");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, fileUploadService.uploadImage(btImage, useType, fileType));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
